package com.doraro.coal.moudle.purch.controller;


import com.doraro.coal.moudle.purch.entity.PurchApply;
import com.doraro.coal.moudle.purch.enums.PermitStatus;
import com.doraro.coal.moudle.purch.service.IPurchApplyService;
import com.doraro.coal.moudle.user.model.entity.Organize;
import com.doraro.coal.moudle.user.model.entity.User;
import com.doraro.coal.moudle.user.service.IOrganizeService;
import com.doraro.coal.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * <p>
 * 燃料采购计划申请单 前端控制器
 * </p>
 *
 * @author doraro
 * @since 2019-10-08
 */
@Controller
public class PurchApplyController {
    @Autowired
    private IOrganizeService organizeService;
    @Autowired
    private IPurchApplyService applyService;
    @GetMapping("/createPR")
    public String createPR(Model model){
        final User userEntity = ShiroUtils.getUserEntity();
        final Organize organize = organizeService.lambdaQuery().eq(Organize::getId, userEntity.getOrgId()).one();
        model.addAttribute("organize", organize.getName());
        return "purchaseRequisitions/creatPR";
    }
    @GetMapping("/updatePR")
    public String updatePR(@RequestParam String id, Model model){
        final PurchApply purch = applyService.getById(id);
        model.addAttribute("p",purch);
        return "purchaseRequisitions/updatePR";
    }

    @GetMapping("/auditPR")
    public String auditPR(){
        return "purchaseRequisitions/auditPR";
    }


    @GetMapping("/queryPR")
    public String queryPR() {
        return "purchaseRequisitions/queryPR";
    }

    @GetMapping("/PRDetails")
    public String detail(@RequestParam String id, Model model){
        final PurchApply purch = applyService.getById(id);
        model.addAttribute("p",purch);
        return "purchaseRequisitions/PRDetails";
    }

    @GetMapping("/auditPRDetail/{id}")
    public String auditPRDetail(@PathVariable String id, Model model){
        final PurchApply purch = applyService.getById(id);
        if (!purch.getPermitStatus().equals(PermitStatus.ADUITING)) {
            model.addAttribute("errMsg", "状态不正确的燃料采购计划申请单");
            return "error/custom";
        }
        model.addAttribute("p",purch);
        return "purchaseRequisitions/auditPRDetails";
    }
}

